/**	
 * @author 
 */

//	Put common scripts here


//	Uncomment the below line if WEBROOT is not setting in default.thtml
//	var WEBROOT = '/tdcs_adm_web/web/';
//  If cakePHP wrbroot folder is the server DocumentRoot
//	var WEBROOT = '/';
	
var selectedRecords = '?ids=';

Event.observe(window, 'load', function(event) {
		
	if (screen.width < 1024 || screen.height < 768) {
		if(window.location.href.match("pages/screen_resolution") != null) {
			window.location = WEBROOT+'pages/screen_resolution';  
		}
	}
	if (!navigator.cookieEnabled) {
		if(window.location.href.match("pages/cookies_disabled") == null) {
			window.location = WEBROOT+'pages/cookies_disabled';
		}  
	}

});

var toggleSelection = function(id, selected) {

	var searchString  = ''+id+';';
	var replaceString = id+';';
	if(selected){
		if(selectedRecords.search(searchString) == -1) {
			selectedRecords += replaceString;
		}
	} else {
		selectedRecords = selectedRecords.replace(searchString, '')
	}
	if ($('viewAllSelected')) {
		$('viewAllSelected').search = selectedRecords;
	}
	if ($('deleteAllSelected')) {
		$('deleteAllSelected').search = selectedRecords;
	}
	 
}

var checkAll = function() {
	
	var inputs  = document.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].type == 'checkbox') {
			if(!inputs[i].checked) {
				inputs[i].checked = true;
				toggleSelection(inputs[i].value, true);
			}
		}
	}
	
}

var uncheckAll = function() {
	
	var inputs  = document.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].type == 'checkbox') {
			if (inputs[i].checked) {
				inputs[i].checked = false;
				toggleSelection(inputs[i].value, false);
			}
		}
	}
	
}

var checkReportName = function(){
	if ($('reportName')) {
		$('reportName').value = $('reportName').value.replace(/^\s+|\s+$/,'');; 
		if ($('reportName').value) {
			return true;
		} else {
			alert("plese enter a Name for this report");
			$('reportName').focus();
			return false;
		}
	} else {
		return false;
	}
}

var submitFormViaJavaScript = function(formToBeSubmitted) {
	
	// Add a form field indicating that this form is submitted via JavaScript 
	var inputField = document.createElement('INPUT');
	inputField.setAttribute('type', 'hidden');
	inputField.setAttribute('name', 'data[JavaScriptCheck]');
	inputField.value = 1;
	formToBeSubmitted.appendChild(inputField);
	// Submit the form
	formToBeSubmitted.submit();
	
}

function test() {
	alert('Test!!');
}